package com.erp.produce.controller;

import com.erp.common.ResponseMessage;
import com.erp.common.controller.BaseController;
import com.erp.produce.entity.ProductionTechnology;
import com.erp.produce.service.ProductionTechnologyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("生产工艺信息接口")
@RestController
@RequestMapping("/pt")
public class ProductionTechnologyController implements BaseController<ProductionTechnology> {

    @Autowired
    ProductionTechnologyService service;


    @ApiOperation("根据产品id获取对应的生产工艺")
    @GetMapping("/list/{goodsId}")
    public ResponseMessage appropriatePT(@PathVariable("goodsId") Long goodsId){
        List<String> names = service.pTNameByGoodsId(goodsId);
        if (names.size() > 0){
            return new ResponseMessage(names);
        }
        return ResponseMessage.fail("无相关生产工艺");
    }


    @ApiOperation("新增实体信息")
    @PostMapping("/add")
    @Override
    public ResponseMessage add(ProductionTechnology entity) {
        System.out.println(entity);
        if (service.add(entity)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("新增生产工艺失败");
    }

    @ApiOperation("删除实体信息（支持多选）")
    @PostMapping("/list/delete")
    @Override
    public ResponseMessage deleteSome(@RequestParam(value = "idArray[]")Long[] ids) {
        if (service.deleteIds(ids)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("删除生产工艺失败");
    }

    @ApiOperation("更新实体信息")
    @PostMapping("/update")
    @Override
    public ResponseMessage update(ProductionTechnology entity) {
        if (service.update(entity)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("更新生产工艺失败");
    }

    @ApiOperation("分页展示实体信息")
    @GetMapping("/list/{page}/{size}")
    @Override
    public ResponseMessage listPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        List<ProductionTechnology> list = service.listPage(page, size);
        if (list.size()>0){
            return new ResponseMessage(list);
        }
        return ResponseMessage.fail("无相关生产工艺信息");
    }

    @ApiOperation("分页展示实体信息（带模糊查询）")
    @GetMapping("/list/name/{page}/{size}")
    @Override
    public ResponseMessage listPageLike(@PathVariable("page") int page, @PathVariable("size")int size, String column) {
        List<ProductionTechnology> list = service.listPageLike(page, size, column);
        if (list.size() > 0){
            return new ResponseMessage(list);
        }
        return ResponseMessage.fail("无相关生产工艺信息");
    }
}
